﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;

namespace SqlPrompt
{
    class ConsoleUtil
    {
        public static void WriteResourceToConsole(string resourceName)
        {
            string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();

            using (StreamReader rs = new StreamReader(
                Assembly.GetExecutingAssembly().GetManifestResourceStream("SqlPrompt.Resources." + resourceName)))
            {
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.BackgroundColor = ConsoleColor.Black;

                string data = rs.ReadToEnd();

                string[] parts = Regex.Split(data, "<(.*?)>");
                foreach (string part in parts)
                {
                    string tag = part.ToLower();
                    if (part.Length > 3 && tag.IndexOf("fc:") == 0)
                    {
                        string color = tag.Substring(3);
                        Console.ForegroundColor = GetConsoleColorFromString(color);
                    }
                    else if (tag == "/fc")
                    {
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                    else if (part.Length > 3 && tag.IndexOf("bc:") == 0)
                    {
                        string color = tag.Substring(3);
                        Console.BackgroundColor = GetConsoleColorFromString(color);
                    }
                    else if (tag == "/bc")
                    {
                        Console.BackgroundColor = ConsoleColor.Black;
                    }
                    else
                    {
                        string output = part
                            .Replace("&gt;", ">")
                            .Replace("&lt;", "<")
                            .Replace("{version}", version);
                        Console.Write(output);
                    }
                }

                Console.WriteLine("");
            }
        }

        public static ConsoleColor GetConsoleColorFromString(string color)
        {
            color = color.ToLower();
            if (color == "black") return ConsoleColor.Black;
            if (color == "blue") return ConsoleColor.Blue;
            if (color == "cyan") return ConsoleColor.Cyan;
            if (color == "darkblue") return ConsoleColor.DarkBlue;
            if (color == "darkcyan") return ConsoleColor.DarkCyan;
            if (color == "darkgray") return ConsoleColor.DarkGray;
            if (color == "darkgreen") return ConsoleColor.DarkGreen;
            if (color == "darkmagenta") return ConsoleColor.DarkMagenta;
            if (color == "darkred") return ConsoleColor.DarkRed;
            if (color == "darkyellow") return ConsoleColor.DarkYellow;
            if (color == "gray") return ConsoleColor.Gray;
            if (color == "green") return ConsoleColor.Green;
            if (color == "magenta") return ConsoleColor.Magenta;
            if (color == "red") return ConsoleColor.Red;
            if (color == "white") return ConsoleColor.White;
            if (color == "yellow") return ConsoleColor.Yellow;
            return ConsoleColor.Gray;
        }
    }
}
